<!DOCTYPE html>
<html lang="zh-cn">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>CSS布局-float</title>
    <style>
      h3 {
        width: 500px;
        height: 20px auto;
      }
      .container {
        width: 500px;
        margin: 20px auto;
        border: 1px solid black;
        padding: 10px;
      }
      .item {
        width: 100px;
        height: 100px;
        border: 1px solid red;
        margin: 10px;
      }
      .left {
        float: left;
      }
      .right {
        float: right;
      }
    </style>
  </head>
  <body>
    <h3>无浮动</h3>
    <div class="container">
      <div class="item">div1</div>
      <div class="item">div2</div>
    </div>
    <h3>左右浮动 float元素会脱离文档流</h3>
    <div class="container">
      <div class="item right">div1</div>
      <div class="item">div2</div>
    </div>
    <h3>父容器塌陷(没高度)了(子元素都浮动)</h3>
    <div class="container">
      <div class="item left">div1</div>
      <div class="item right">div2</div>
    </div>
    <h3>解决方法一 父容器(设置overflow: hidden)</h3>
    <div class="container" style="overflow: hidden">
      <div class="item left">div1</div>
      <div class="item right">div2</div>
    </div>
    <h3>解决方法二 加一个空div, 并在设置clear: both</h3>
    <div class="container">
      <div class="item right">div1</div>
      <div class="item right">div2</div>
      <div style="clear: both"></div>
    </div>
  </body>
</html>
